package nl.infodation.emr.pricingservice.rulegeneration;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import nl.infodation.emr.pricingservice.entity.DomainRule;
import nl.infodation.emr.utility.Constants;

import org.eclipse.emf.mwe.core.resources.ResourceLoaderFactory;
import org.eclipse.emf.mwe2.language.Mwe2StandaloneSetup;
import org.eclipse.emf.mwe2.launch.runtime.Mwe2Runner;
import org.eclipse.emf.mwe2.runtime.workflow.IWorkflowContext;
import org.eclipse.emf.mwe2.runtime.workflow.WorkflowContextImpl;

import com.google.inject.Injector;
import com.ibm.icu.util.Calendar;
import com.vaadin.ui.Component;

public class DrlFileGeneration {
	
	private static final String TRIM_DATE_TIME_PATTERN = "yyyyMMddHHmmss";
	private static final String FULL_DATE_TIME_PATTERN = "yyyy-MM-dd,HH:mm:ss";
	
	private List<DomainRule> rules = new ArrayList<DomainRule>();

	public List<DomainRule> getRules() {
		return rules;
	}

	public void defineRule(DomainRule domainRule) {
		this.rules.add(domainRule);
	}

	public void clearAllRules() {
		rules.clear();
	}
	
	public void generateDrlFile(String filePath, String packagePrefix, String country, Component component) throws IOException {

		// write drl file
		File file = new File(filePath);

		if (!file.isDirectory()) { // is a file
			if (!file.exists()) {
				// add info to file that is invalid
				File directory = file.getParentFile();
				if (!directory.exists()) {
					throw new IOException("Directory does not exist");
				}
			}
		} else { // is a directory
			if (!file.exists()) {
				String path = component.getApplication().getContext()
						.getBaseDirectory().getPath()
						+ Constants.DEFAULT_FILENAME;
				file = new File(path);
			} else {
				file = new File(file.getPath() + Constants.DEFAULT_FILENAME);
			}
		}
		// force overwrite
		if (file.exists()) {
			file.delete();
		}
		Injector i = new Mwe2StandaloneSetup()
				.createInjectorAndDoEMFRegistration();
		Mwe2Runner runner = i.getInstance(MyRunner.class);
		IWorkflowContext context = new WorkflowContextImpl();

		Calendar cal = Calendar.getInstance();
		// Make sure we use our own resource loader to avoid JBoss virtual
		// file system quirks for URI's
		System.setProperty(ResourceLoaderFactory.PARAM_RESOURCELOADER_CLASS,
				ContextFirstResourceLoader.class.getName());
		context.put("drlFileGeneration", this);
		context.put("currentDate", new SimpleDateFormat(FULL_DATE_TIME_PATTERN).format(cal.getTime()));
		context.put("targetDir", file.getParentFile().getAbsolutePath());
		context.put("ruleFile", file.getAbsolutePath());
		context.put("packagePrefix", packagePrefix);
		context.put("country", country);
		runner.run("workflow.GenerateRules", new HashMap<String, String>(),
				context);

	}

	/**
	 * This function will be used in [Template for generating rule files]
	 * Do not delete
	 * @param date
	 * @return
	 */
	public static String formatDate(Date date) {
		return date != null ? new SimpleDateFormat(TRIM_DATE_TIME_PATTERN)
				.format(date) : "";
	}
}
